/*
 * 作者：姚俊
 * 功能：学位论文的逻辑处理
 * 日期：2015-06-30
 */
package admin.service.fruits;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import beanDao.MapToBean;
import teacher.bean.Studegreepaper;
import teacher.bean.Teacher;
import database.dao.MysqlCrud;

public class DegreePaperService {
	
	/**
	 * @category 查询所有学位论文进行分页
	 * @param pageSize int 一个页面要显示pageSize条个数据
	 * @param pageNow int 从pageNow当前的数据来显示
	 * @return papers List<Paper> 返回查询到的论文列表
	 * 
	 */
	public List<Studegreepaper> getSegregateDegreePaperByAll(Studegreepaper degreePaper,int pageNow,int pageSize) {
		
		String sql="select * from studegreepaper ";
		int j=0;
		String sql2=" degId LIMIT "+(pageNow-1)+" ,"+pageSize+" ";
		List<Studegreepaper> degreePapers=new ArrayList<Studegreepaper>();
		List<String> sql1=new ArrayList<String>();
		List<String> paras=new ArrayList<String>();
		
		System.out.println("sql1.sizefsad");

		if(degreePaper!=null)
		{
			if(degreePaper.getStuId()!=null)
			{
				sql1.add(" stuId=? ");
				paras.add(String.valueOf(degreePaper.getStuId()));
			}
			if(degreePaper.getDegname()!=null)
			{
				sql1.add(" degname like ? ");
				paras.add("%"+String.valueOf(degreePaper.getDegname())+"%");
			}
			if(degreePaper.getDegaward()!=null)
			{
				sql1.add(" degaward=? ");
				paras.add(String.valueOf(degreePaper.getDegaward()));
			}
		}
		
		for(int i=0;i<sql1.size();i++)
		{
			if(i==0)
				sql+="where"+sql1.get(i);
			else
				sql+="and"+sql1.get(i);
		}
		System.out.println("sql1.size"+sql1.size());
		
		//加入最后一个,该字符是用于分页
		if(sql1.size()==0)
			sql+=sql2;
		else
			sql+="and"+sql2;
		
		String[] parameter=new String[paras.size()];
		for(int i=0;i<paras.size();i++)
		{
			parameter[i]=paras.get(i);
		}
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		while(lists!=null&&j<lists.size()){
			Map map=lists.get(j);
			degreePapers.add(new MapToBean().MapToStudegreepaper(map));
			j++;
		}
		return degreePapers;
	}
	
	
	
	/**
	 * @category 查询所有学位论文的数量
	 * 
	 */
	public int getCountDegreePaperByAll(Studegreepaper degreePaper) {
		
		String sql="select count(*) from studegreepaper ";
		int count=0;
		List<String> sql1=new ArrayList<String>();
		List<String> paras=new ArrayList<String>();
		
		if(degreePaper!=null)
		{
			if(degreePaper.getStuId()!=null)
			{
				sql1.add(" stuId=? ");
				paras.add(String.valueOf(degreePaper.getStuId()));
			}
			if(degreePaper.getDegname()!=null)
			{
				sql1.add(" degname like ? ");
				paras.add("%"+String.valueOf(degreePaper.getDegname())+"%");
			}
			if(degreePaper.getDegaward()!=null)
			{
				sql1.add(" degaward=? ");
				paras.add(String.valueOf(degreePaper.getDegaward()));
			}
		}
		
		for(int i=0;i<sql1.size();i++)
		{
			if(i==0)
				sql+="where"+sql1.get(i);
			else
				sql+="and"+sql1.get(i);
		}
		
		String[] parameter=new String[paras.size()];
		
		for(int i=0;i<paras.size();i++)
		{
			parameter[i]=paras.get(i);
		}
		
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql, parameter);
		Map map=lists.get(0);
		count=Integer.parseInt(map.get(0).toString());
		return count;
	}
	
	/**
	 * @category 查询所有学位论文进行分页
	 * @param pageSize int 一个页面要显示pageSize条个数据
	 * @param pageNow int 从pageNow当前的数据来显示
	 * @return papers List<Paper> 返回查询到的论文列表
	 * 
	 */
	public List<Studegreepaper> getSegregateDirectionDegreePaperByAll(Studegreepaper degreePaper,Teacher teacher,int pageNow,int pageSize) {
		
		String sql="select * from studegreepaper as paper,teacher as tea,students as stu where tea.teaId=stu.teaId and stu.stuId=paper.stuId and tea.direction=? ";
		int j=0;
		String sql2=" degId LIMIT "+(pageNow-1)+" ,"+pageSize+" ";
		List<Studegreepaper> degreePapers=new ArrayList<Studegreepaper>();
		List<String> sql1=new ArrayList<String>();
		List<String> paras=new ArrayList<String>();
		paras.add(teacher.getDirection().toString());

		if(degreePaper!=null)
		{
			if(degreePaper.getStuId()!=null)
			{
				sql1.add(" stuId=? ");
				paras.add(String.valueOf(degreePaper.getStuId()));
			}
			if(degreePaper.getDegname()!=null)
			{
				sql1.add(" degname like ? ");
				paras.add("%"+String.valueOf(degreePaper.getDegname())+"%");
			}
			if(degreePaper.getDegaward()!=null)
			{
				sql1.add(" degaward=? ");
				paras.add(String.valueOf(degreePaper.getDegaward()));
			}
		}
		
		for(int i=0;i<sql1.size();i++)
		{
				sql+="and"+sql1.get(i);
		}
		System.out.println("sql1.size"+sql1.size());
		
		//加入最后一个,该字符是用于分页
			sql+="and"+sql2;
		
		String[] parameter=new String[paras.size()];
		for(int i=0;i<paras.size();i++)
		{
			parameter[i]=paras.get(i);
		}
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		while(lists!=null&&j<lists.size()){
			Map map=lists.get(j);
			degreePapers.add(new MapToBean().MapToStudegreepaper(map));
			j++;
		}
		return degreePapers;
	}
	
	/**
	 * @category 根据名称对查询所有学位论文的数量
	 * 
	 */
	public int getCountDirectionDegreePaperByAll(Studegreepaper degreePaper,Teacher teacher) {
		
		String sql="select count(*) from studegreepaper as paper,teacher as tea,students as stu where tea.teaId=stu.teaId and stu.stuId=paper.stuId and tea.direction=? ";
		int count=0;
		List<String> sql1=new ArrayList<String>();
		List<String> paras=new ArrayList<String>();
		paras.add(teacher.getDirection().toString());
		
		if(degreePaper!=null)
		{
			if(degreePaper.getStuId()!=null)
			{
				sql1.add(" stuId=? ");
				paras.add(String.valueOf(degreePaper.getStuId()));
			}
			if(degreePaper.getDegname()!=null)
			{
				sql1.add(" degname like ? ");
				paras.add("%"+String.valueOf(degreePaper.getDegname())+"%");
			}
			if(degreePaper.getDegaward()!=null)
			{
				sql1.add(" degaward=? ");
				paras.add(String.valueOf(degreePaper.getDegaward()));
			}
		}
		
		for(int i=0;i<sql1.size();i++)
		{
				sql+="and"+sql1.get(i);
		}
		
		String[] parameter=new String[paras.size()];
		
		for(int i=0;i<paras.size();i++)
		{
			parameter[i]=paras.get(i);
		}
		
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql, parameter);
		Map map=lists.get(0);
		count=Integer.parseInt(map.get(0).toString());
		return count;
	}
		
}
